<template>
   <!--登录表单的容器-->
  <div class="login_container">
    <!--登录区域-->
    <div class="login_box">
      <!--头像-->
      <div class="avatar_box">
        <img src="../../assets/images/bf.jpg">
      </div>
      <!--表单-->
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名称" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入登录密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="verifyCode">
          <div class="verifyCode_box">
            <el-input v-model="loginForm.verifyCode" placeholder="请输入手机验证码" prefix-icon="el-icon-mobile" class="verifyCode"></el-input>
            <!-- <img src="../../assets/images/yzm.png" alt="" class="verifyCode_img"> -->
              <img src="http://192.168.43.128:8888/captcha" alt="" class="verifyCode_img">
          </div>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="submitForm('loginForm')">登 录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>



</template>

<script>
import { mapMutations } from "vuex";
import { USERLOGIN, USERLOGOUT } from "../../store/types";
import {login} from '../../api/user.js'
import {request} from '../../utils/index.js'
export default {
data () {
    return {
         loginForm: {
          username: 'wuchao',
          password: '123456',
          verifyCode: '143'
        },loginRules: {
          username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' },
            { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
          ],
          verifyCode: [
            { required: true, message: '请输入验证码', trigger: 'blur' }
          ]
        }
    }
},methods: {
  ...mapMutations([USERLOGIN, USERLOGOUT]),
     submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {//这些都是ele上面的校验全套
          let result=await login(this.loginForm.username,this.loginForm.password,this.loginForm.verifyCode);
        console.log(result);
        const token=result.data.token;
        // window.localStorage.setItem("token",token);//存入本地
        // //this.$store.state.token=token;//官方不推荐
        // this.$store.state.token=token;
        // this.$store.commit('setToken', token);
        // this.$router.push({
        //         path:'/main',
        //     });
         if (token != "") {
            // 保存token 用于权限验证
            this[USERLOGIN](token);
            this.$router.push("/main");
          } else {
            this.$message({
              message: "你的账号密码错误",
              type: "error",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
}
}
</script>
<style lang="less" scoped>

 .login_container {
    height: 100%;
    background-color: aquamarine;
  }

  .login_box {
    width: 450px;
    height: 380px;
    background-color: #FFFFFF;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box{
      width: 130px;
      height: 130px;
      border: 1px solid #EEEEEE;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      margin: -65px auto;
      background-color: #FFFFFF;

      img{
        width: 130px;
        height: 130px;
        border-radius: 50%;
        background-color: #EEEEEE;
      }
    }

    .login_form{
      position: absolute;
      bottom: 0px;
      width: 100%;
      padding: 0px 20px;
      box-sizing: border-box;

      .login_btn{
        display: flex;
        justify-content: flex-end;
      }

      .verifyCode_box{
        display: flex;
        .verifyCode{
          width: 70%;
          justify-content: left;
        }

        .verifyCode_img{
          width: 30%;
          height: 45px;
          justify-content: flex-end;
        }
      }
    }
  }
</style>>

</style>